package f3.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class CoreUtil {
	public static String downloadUrlText(String strUrl){
		String ret = "";
		URL url;
		try {
			url = new URL(strUrl);
			 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			    String str;
			    while ((str = in.readLine()) != null) {
			      //System.out.println(str); 
			    	ret+=" " + str.trim()+" \n";
			    }
			    in.close();	
			
//			ret = readTextFromInputStream(url.openStream(), "GBK");
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	public static String readTextFile(String filePath){
		String ret = null;
		ret=readTextFile(filePath, "UTF-8");
		return ret;
	}
	
	public static String readTextFile(String filePath,String charSet){
		String ret = null;
		FileInputStream fis =null;
		try {
		    fis = new FileInputStream(filePath);
			int len = fis.available();
			byte [] buf = new byte[len];
			fis.read(buf);
			
			ret = new String(buf,charSet);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}
	public static String readTextFromInputStream(InputStream is,String charSet){
		String ret = null;
		try {
			int len = is.available();
			byte [] buf = new byte[len];
			is.read(buf);
			ret = new String(buf,charSet);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	public static void main(String[] args) {
		String boo = downloadUrlText("http://market.finance.sina.com.cn/downxls.php?date=2013-07-11&symbol=sz300223");
		System.out.println(boo);
		
		saveTextToFile("d:\\future3\\sz300223_20130711.txt", boo);
		
	}
	
	public static boolean saveTextToFile(String filePath,String text ){
		boolean ret = false;
		ret = saveTextToFile(filePath, text,"UTF-8");
		return ret;
	}
 
	public static boolean saveTextToFile(String filePath,String text,String charSet){
		boolean ret = false;
		FileOutputStream fos= null;
		try {
			fos = new FileOutputStream(filePath,true);
			fos.write(text.getBytes(charSet));
			fos.flush();
			ret = true;
			//System.out.println("[saveTextToFile successful] " + filePath );
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ret = false;
			}
		}
		return ret;
	}
}
